package com.model;

import java.util.*;
import java.sql.*;

import com.javabean.Doc;

public class DocListService {
	public Vector getAllDocList(){
		Vector v = new Vector();
		try{
			Connection con = DataBase.getConnection();
		
			String sql = "SELECT docId,docName,userName,docDateOfUp,docType FROM docmansysdb.doc,docmansysdb.user WHERE docOwner = userId ORDER BY docDateOfUp";
			Statement stat = con.createStatement();
			ResultSet rs = stat.executeQuery(sql);
			while(rs.next()){
				Doc doc = new Doc();
				doc.setDocId(rs.getLong("docId"));
				doc.setDocName(rs.getString("docName"));
				doc.setDocOwner(rs.getString("userName"));
				doc.setDocDateOfUp(rs.getString("docDateOfUp"));
				doc.setDocType(rs.getString("docType"));
				
				v.add(doc);
			}
			
			rs.close();
			con.close();
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}catch(Exception e2){
			e2.printStackTrace();
			return null;
		}
		return v;
	}
	
	public Vector getAllDocListByUserId(long userId){
		Vector v = new Vector();
		try{
			Connection con = DataBase.getConnection();
		
			String sql = "SELECT docId,docName,userName,docDateOfUp,docType FROM docmansysdb.doc,docmansysdb.user WHERE userId = docOwner AND userId = " + userId + " ORDER BY docDateOfUp";
			Statement stat = con.createStatement();
			ResultSet rs = stat.executeQuery(sql);
			while(rs.next()){
				Doc doc = new Doc();
				doc.setDocId(rs.getLong("docId"));
				doc.setDocName(rs.getString("docName"));
				doc.setDocOwner(rs.getString("userName"));
				doc.setDocDateOfUp(rs.getString("docDateOfUp"));
				doc.setDocType(rs.getString("docType"));
				
				v.add(doc);
			}
			
			rs.close();
			con.close();
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}catch(Exception e2){
			e2.printStackTrace();
			return null;
		}
		return v;
	}
}
